package Util;

import java.util.Date;

public class Sign {

    /**
     * 创建签名
     *
     * @param timestamp 时间错
     * @return 签名字符串
     */
    private static String CreateSign(long timestamp) {
        if (timestamp == 0) {
            timestamp = TimeUtil.Date2TimeStamp(new Date());
        }

        String key = "5F9DF7CCEDF5F0F58D9D8F9C9C950F10";
        return MD5Util.GetMD5(key + timestamp);
    }

    /**
     * 验证签名
     *
     * @param timestamp 时间戳
     * @param sign      签名字符串
     * @return true验证通过，false验证失败
     */
    public static boolean VerifySign(long timestamp, String sign) {

        Date now = new Date();
        Date date = TimeUtil.TimeStamp2Date(timestamp);

        if (date.getTime() < TimeUtil.addMinutesToDate(now, -120).getTime()) {
            return false;
        }

        return sign.toLowerCase().equals(Sign.CreateSign(timestamp).toLowerCase());
    }
}